﻿using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	/*	Explanations, KEEP THIS AS COMMENTS:
		t = timer
		max = max amount, used to limit things
		default = used to reset timers
	*/

	public static float defaultHPt = 10f; // default HPtimer
	public static float defaultSPt = 3f; // default SPtimer
	public static float defaultOPt = 2f; // default OPtimer
	public static float defaultCrewt = 20f; // default crewtimer
	public static int playerScrap = 0;
	public static int playerHP = 100;
	public static int playerHPmax = 90 + ShipModuleData.hullLv * 10; // current max hull points
	public static int lowHPth = 50;
	public static int playerSP = 100;
	public static int playerSPmax = 100; // current max shield points
	public static int playerEPuse = 0;
	public static int playerEPmax = ShipModuleData.reactorOutput; // current max energy capacity points
	public static int playerOP = 100; // current oxygen percentage
	public static int playerOPmax = 100; // current max oxygen percentage (always 100)
	public static int playerOPcritical = 10; // oxygen critical level percentage, crew loss starts
	public static int playerCrew = 3; // current organic crew amount
	public static int playerBotCrew = 1; // current synthetic crew amount
	public static int playerCrewMax = ShipModuleData.crewSlots; // current player crew slots
	public static int playerBotCrewMax = ShipModuleData.botCrewSlots;
	public static int playerCargoSlots = ShipModuleData.cargoLv * 100;
	public static float playerSPt = defaultSPt;
	public static float playerHPt = defaultHPt;
	public static float playerOPt = defaultOPt;
	public static float playerCrewt = defaultCrewt;

	public static bool player_destroyed = false;

	public static bool hullAlert = false;
	public static bool oxygenAlert = false;
	public static bool shieldsLowAlert = false;
	public static bool shieldsOffAlert = false;
	public static bool dampenersOffAlert = false;

	public GUIStyle scrap_style;
	public GUIStyle gameover_style;
	public GUIStyle HP_style;
	public GUIStyle SP_style;
	public GUIStyle EP_style;
	public GUIStyle OP_style;
	public GUIStyle alert_style;


	void Start()
	{
		// nämä pitää resetoida käsin, koska ne ovat staattisia
		playerHPmax = 90 + ShipModuleData.hullLv * 10;
		playerHP = playerHPmax;
		playerSPmax = 100;
		playerSP = playerSPmax;
		playerEPmax = ShipModuleData.reactorOutput;
		playerOP = 100;
		playerScrap = 0;
		player_destroyed = false;
		playerSPt = defaultSPt;
		playerHPt = defaultHPt;
		playerOPt = defaultOPt;
		playerCrewt = defaultCrewt;

		hullAlert = false;
		oxygenAlert = false;
		shieldsLowAlert = false;
		shieldsOffAlert = false;

		ShipModuleData.enginesON = true;
		ShipModuleData.repairModON = true;
		ShipModuleData.shieldGeneratorON = true;
		ShipModuleData.lifeSupportON = true;
		ShipModuleData.radarON = true;
		ShipModuleData.alarmON = true;

		ShipModuleData.hullLv = ShipModuleData.defaultHullLv;
		ShipModuleData.repairModLv = ShipModuleData.defaultRepairModLv;
		ShipModuleData.shieldGeneratorLv = ShipModuleData.defaultShieldGeneratorLv;
		ShipModuleData.lifeSupportLv = ShipModuleData.defaultShieldGeneratorLv;
		ShipModuleData.hullPlatingLv = ShipModuleData.defaultHullPlatingLv;
		ShipModuleData.enginesLv = ShipModuleData.defaultEnginesLv;
		ShipModuleData.radarLv = ShipModuleData.defaultRadarLv;
		ShipModuleData.reactorLv = ShipModuleData.defaultReactorLv;
		ShipModuleData.crewLv = ShipModuleData.defaultCrewLv;
		ShipModuleData.alarmLv = ShipModuleData.defaultAlarmLv;
		ShipModuleData.cargoLv = ShipModuleData.defaultCargoLv; 
	}

	// GUI
	void OnGUI() {

		if (player_destroyed == false)
		{
			// disabling GUI if paused
			if (PauseMenu.paused == true)
				return;

			//
			ShipModuleData.alarmON = GUI.Toggle(new Rect(2, 160, 146, 24), ShipModuleData.alarmON, "(" + ShipModuleData.alarmEP + ")\t\t" + "Alarms");
			ShipModuleData.repairModON = GUI.Toggle(new Rect(2, 185, 146, 24), ShipModuleData.repairModON, "(" + ShipModuleData.repairModEP + ")\tLv." + ShipModuleData.repairModLv + " Repairs");
			ShipModuleData.shieldGeneratorON = GUI.Toggle(new Rect(2, 210, 146, 24), ShipModuleData.shieldGeneratorON, "(" + ShipModuleData.shieldGeneratorEP + ")\tLv." + ShipModuleData.shieldGeneratorLv + " Shields");
			ShipModuleData.lifeSupportON = GUI.Toggle(new Rect(2, 235, 146, 24), ShipModuleData.lifeSupportON, "(" + ShipModuleData.lifeSupportEP + ")\tLv." + ShipModuleData.lifeSupportLv + " Life S.");
			ShipModuleData.radarON = GUI.Toggle(new Rect(2, 260, 146, 24), ShipModuleData.radarON, "(" + ShipModuleData.radarEP + ")\tLv." + ShipModuleData.radarLv + " Sensors");
			ShipModuleData.enginesON = GUI.Toggle(new Rect(2, 285, 144, 24), ShipModuleData.enginesON, "(" + ShipModuleData.enginesEP + ")\tLv." + ShipModuleData.enginesLv + " Engines");
			ShipModuleData.dampenersON = GUI.Toggle(new Rect(2, 310, 146, 24), ShipModuleData.dampenersON, "(" + ShipModuleData.dampenersEP + ")\t\t" + "Dampeners");


			GUI.Label (new Rect(4,4,100,100), "Scrap: " + playerScrap, scrap_style);
			GUI.Label (new Rect(104,4,100,100), "Speed (m/s): \t" + PlayerControls.speedo, scrap_style);
			GUI.Label (new Rect(40,24,100,100), "Hull: " + playerHP + " / " + playerHPmax, HP_style);
			GUI.Label (new Rect(32,48,100,100), "Shields: " + playerSP + " / " + playerSPmax, SP_style);
			GUI.Label (new Rect(24,72,100,100), "Energy: " + playerEPuse + " / " + playerEPmax, EP_style);
			GUI.Label (new Rect(18,96,100,100), "Oxygen: " + playerOP + "%", OP_style);
			GUI.Label (new Rect(18,116,100,100), "Organic Crew: " + playerCrew + " / " + playerCrewMax, OP_style);
			GUI.Label (new Rect(18,136,100,100), "Synthetic Crew: " + playerBotCrew + " / " + playerBotCrewMax, OP_style);

			// speedometer

			

			// alerts
			if (ShipModuleData.alarmON == true)
			{
				//alerts on
				if (playerOP < playerOPmax/4)
					oxygenAlert = true;
				if (playerHP < lowHPth)
					hullAlert = true;
				if (playerSP < playerSPmax/5)
					shieldsLowAlert = true;
				if (ShipModuleData.shieldGeneratorON == false)
					shieldsOffAlert = true;
				//alerts off
				if (playerOP >= playerOPmax/4)
					oxygenAlert = false;
				if (playerHP >= playerHPmax/2)
					hullAlert = false;
				if (playerSP >= playerSPmax/5 || shieldsOffAlert == true)
					shieldsLowAlert = false;
				if (ShipModuleData.shieldGeneratorON == true)
					shieldsOffAlert = false;
				if (ShipModuleData.dampenersON == true)
					dampenersOffAlert = false;
				if (ShipModuleData.dampenersON == false)
					dampenersOffAlert = true;
			}
			if (ShipModuleData.alarmON == false)
			{
					oxygenAlert = false;
					hullAlert = false;
					shieldsLowAlert = false;
					shieldsOffAlert = false;
					dampenersOffAlert = false;
			}
			if (oxygenAlert == true)
				GUI.Label (new Rect(160,239,100,100), "Oxygen levels critical!", alert_style);
			if (hullAlert == true)
				GUI.Label (new Rect(160,189,100,100), "Hull integrity compromised!", alert_style);
			if (shieldsLowAlert == true)
				GUI.Label (new Rect(160,214,100,100), "Critical shields!", alert_style);
			if (shieldsOffAlert == true)
				GUI.Label (new Rect(160,214,100,100), "Shield generator offline!", alert_style);
			if (dampenersOffAlert == true)
				GUI.Label (new Rect(160,314,100,100), "Dampeners offline!", alert_style);

		}
	

		

		//printataan GAME OVER keskelle ruutua
		if (player_destroyed == true)
		{
			float x = Screen.width/2;
			float y = Screen.height/2;
			GUI.Label (new Rect (x, y, 100, 100), "GAME OVER", gameover_style);

			if (GUI.Button (new Rect (x - 120, y + 120, 120, 100), "RESTART")) {
				Application.LoadLevel ("level_1");
			}
			
			if (GUI.Button (new Rect(x, y + 120, 120, 100), "Main Menu")) {
				Application.LoadLevel("mainmenu");
			}

		}


	}

	void Update() {

		//player stats
		playerHPmax = (90 + ShipModuleData.hullLv * 10);



		// varmistukset
		if (playerCrew > playerCrewMax) //varmistetaan ettei liikaa orgaanista crewiä
			playerCrew = playerCrewMax;

		if (playerBotCrew > playerBotCrewMax) //varmistetaan ettei liikaa synteettistä crewiä
			playerBotCrew = playerBotCrewMax;

		if (playerSP < (-10)) //varmistetaan ettei SP mene liian alas
			playerSP = (-10);

		if (playerSP > playerSPmax) // varmistetaan ettei SP mene yli maksimin
			playerSP = playerSPmax;

		if (ShipModuleData.enginesON == false)
			ShipModuleData.dampenersON = false;

		if (playerHP > playerHPmax)
			playerHP = playerHPmax;


		// restartti
		if (Input.GetKey (KeyCode.R)) {
			Application.LoadLevel (Application.loadedLevelName);
		}

		// EP usage
		playerEPuse = 0;

		playerEPuse = playerEPuse + ShipModuleData.botCrewEP;

		if (ShipModuleData.repairModON == true)
			playerEPuse = playerEPuse + ShipModuleData.repairModEP;
		if (ShipModuleData.enginesON == true)
			playerEPuse = playerEPuse + ShipModuleData.enginesEP;
		if (ShipModuleData.shieldGeneratorON == true)
			playerEPuse = playerEPuse + ShipModuleData.shieldGeneratorEP;
		if (ShipModuleData.lifeSupportON == true)
			playerEPuse = playerEPuse + ShipModuleData.lifeSupportEP;
		if (ShipModuleData.radarON == true)
			playerEPuse = playerEPuse + ShipModuleData.radarEP;
		if (ShipModuleData.alarmON == true)
			playerEPuse = playerEPuse + ShipModuleData.alarmEP;
		if (ShipModuleData.dampenersON == true)
			playerEPuse = playerEPuse + ShipModuleData.dampenersEP;
		
		if (playerEPuse > playerEPmax) // shutting down all systems if reactor overloads
		{
			ShipModuleData.enginesON = false;
			ShipModuleData.repairModON = false;
			ShipModuleData.shieldGeneratorON = false;
			ShipModuleData.lifeSupportON = false;
			ShipModuleData.radarON = false;
			ShipModuleData.alarmON = false;
			ShipModuleData.dampenersON = false;
		}

		// jos hull pointit tai organic crew loppuvat, loppuu peli
		if (playerHP <= 0 || playerCrew < 1)
		{
			player_destroyed = true;
		}


	}
}
